<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" size="default" @click="quit">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
          <div class="toggle-button" @click="toggle">|||</div>
        <!-- 左侧菜单布局 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409FFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition='false'
          router
          :default-active='select'
          @select="open"
        >
          <!-- 一级菜单 的模板区 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key='item.id'>
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+childItem.path" v-for="childItem in item.children" :key="childItem.id" @click="activePath('/'+childItem.path)">
              <template slot="title" >
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{childItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
          <!-- 放置路由占位符 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      menuList: [],
      iconObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-tools',
        101: 'el-icon-s-goods',
        102: 'el-icon-message-solid',
        145: 'el-icon-s-order'
      },
      isCollapse: false,
      select: ''
    }
  },
  created () {
    this.getMenuList()
    const activePath = window.sessionStorage.getItem('activePath')
    if (activePath && activePath !== '') {
      this.select = activePath
    }
  },
  methods: {
    //   退出
    quit () {
      // 清空Token
      window.sessionStorage.clear('Token')
      // 跳转到登录页面
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击展开折叠左侧菜单栏
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    // 打开的菜单
    open (value) {
      this.select = value
    },
    activePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style scoped lang='less'>
.toggle-button{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373f41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  padding-left: 0;
  > div {
    display: flex;
    align-items: center;
    img {
      height: 40px;
    }
    span {
      padding-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;

  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}

</style>
